package pers.feng1240.blog.interceptor;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class ManagerInterceptor implements HandlerInterceptor{

	//指定允许通过的后缀请求 xxx.do 是一个入口
	private List<String> allowedPass;
	//注入条件
	public void setAllowedPass(List<String> allowedPass) {
		this.allowedPass = allowedPass;
	}
	

	@Override
	public boolean preHandle(HttpServletRequest req, HttpServletResponse resp,
			Object arg2) throws Exception {

			//获取url路径
			String url = req.getRequestURI().toString();
			
			//检测用户登录 直接通过拦截器
			if(req.getSession().getAttribute("manager")!=null) return true;
				
		     	/**
				 * 拦截指定后缀的请求
				 */
				for(String temp:allowedPass){
					System.out.println(temp);
					if(url.endsWith(temp)){
						System.out.println("通过...");
						return true; //允许下一个拦截器
					}
				}
				
			String sendUrl = req.getContextPath()+"/login.jsp";
			resp.sendRedirect(sendUrl);
			return false;
	}
	
	@Override
	public void afterCompletion(HttpServletRequest arg0,
			HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		
	}

	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
			Object arg2, ModelAndView arg3) throws Exception {
	}


}

